package com.test.down.utils;

import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.test.down.MyApplication;
import com.test.down.beans.LiveBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class DownLoadInfouTIL {

    private static DownLoadInfouTIL downLoadInfouTIL;

    private DownLoadInfouTIL(){}

    public static DownLoadInfouTIL getInstance(){
        if(downLoadInfouTIL==null){
            downLoadInfouTIL=new DownLoadInfouTIL();
        }
        return downLoadInfouTIL;
    }

    public static String m3u8DataKey="m3u8List";
    private JSONObject m3u8LoadList;

    /**
     * 保存下载信息
     * @param info
     */
    public void insetItemToDownLoadList(LiveBean info){
        try {
            JSONArray list=getM3u8DownLoadListData();
            if(list==null || hasInDownList(info.playUrl)){
                Toast.makeText(MyApplication.mContext,"加入下载列表失败,请稍后再试",Toast.LENGTH_SHORT).show();
            }else {
                JSONObject item=standItemData(info);
                if(item!=null) {
                    list.put(item);
                    m3u8LoadList.put("data",list);
                    MyApplication.saveData(MyApplication.mContext, m3u8DataKey, m3u8LoadList.toString());
                    Toast.makeText(MyApplication.mContext,"加入下载列表",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MyApplication.mContext,"加入下载列表失败,请稍后再试",Toast.LENGTH_SHORT).show();
                }
            }
        }catch (Exception e){
            Toast.makeText(MyApplication.mContext,"加入下载列表失败,请稍后再试",Toast.LENGTH_SHORT).show();
        }
    }

    private JSONArray getM3u8DownLoadListData(){
        try {
            String m3u8Data = MyApplication.obtainData(MyApplication.mContext, m3u8DataKey, "");
            JSONObject object=null;
            if (TextUtils.isEmpty(m3u8Data)) {
                object=new JSONObject();
            }else {
                object=new JSONObject(m3u8Data);
            }
            if(!object.has("data")){
                object.put("data",new JSONArray());
            }
            m3u8LoadList=object;
            return object.getJSONArray("data");
        }catch (Exception e){
            return null;
        }
    }

    private JSONObject standItemData(LiveBean info){
        try {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("playUrl",info.playUrl);
            jsonObject.put("liveCover",info.liveCover);
            jsonObject.put("title",info.title);
            return jsonObject;
        }catch (Exception e){
            Toast.makeText(MyApplication.mContext,"加入下载列表失败,请稍后再试",Toast.LENGTH_SHORT).show();
            return null;
        }
    }

    /**
     * 是否已在缓存列表中
     * @return
     */
    private boolean hasInDownList(String url){
        try{
            JSONArray list=getM3u8DownLoadListData();
            if(list==null || list.length()==0){
                return false;
            }else {
                for (int i=0;i<list.length();i++){
                    if(list.getJSONObject(i).getString("playUrl").equals(url)){
                        return true;
                    }
                }
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 删除历史记录
     * @param info
     */
    public boolean deleteItemInDownLoadList(LiveBean info){
        try{
            JSONArray list=getM3u8DownLoadListData();
            if(list==null || list.length()==0){
                return false;
            }else {
                boolean isDelete=false;
                for (int i=0;i<list.length();i++){
                    if(list.getJSONObject(i).getString("playUrl").equals(info.playUrl)){
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                            list.remove(i);
                        }
                        isDelete=true;
                        break;
                    }
                }
                if(isDelete) {
                    m3u8LoadList.put("data", list);
                    MyApplication.saveData(MyApplication.mContext, m3u8DataKey, m3u8LoadList.toString());
                }else {

                }
                return isDelete;
            }
        }catch (Exception e){
            Toast.makeText(MyApplication.mContext,"删除失败,稍后再试",Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    /**
     * 读取历史列表
     * @return
     */
    public ArrayList<LiveBean> readVideoHistory(){
        ArrayList<LiveBean> infos=new ArrayList<>();
        try {
            JSONArray array=getM3u8DownLoadListData();
            if(array!=null && array.length()>0){
                for (int i=0;i<array.length();i++){
                    LiveBean info=new Gson().fromJson(array.getJSONObject(i).toString(),LiveBean.class);
                    infos.add(info);
                }
            }
        }catch (Exception e) {
        }
        return infos;
    }

}
